package com.service.impl;

import com.entity.Book;
import com.entity.Branch;
import com.entity.Stores;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.mapper.BookMapper;
import com.mapper.BranchMapper;
import com.mapper.StoresMapper;
import com.service.BookService;
import com.service.BranchService;
import com.vo.Page;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @ fileName:BookServiceImpl
 * @ description:
 * @ author:dzh
 * @ createTime:2022/4/29 17:00
 * @ version:1.0.0
 */
@Service
public class BookServiceImpl implements BookService {
    @Resource
    private BookMapper mapper;
    @Resource
    private BranchMapper branchMapper;
    @Resource
    private StoresMapper storesMapper;
    @Override
    public PageInfo<Book> listByParam(Page<Book> page) {
        PageHelper.startPage(page.getPageNum(),page.getPageSize());
        List<Book> books = mapper.listAll(page.getData());
        return new PageInfo<>(books);
    }

    @Override
    @Transactional
    public int insert(Book book) {
        int sum= 0;
        if (book.getBookImg() == null && book.getBookImg() == "") {
            book.setBookImg("");
        }
        if (book.getAuthor()==null && book.getAuthor()==""){
            book.setAuthor("佚名");
        }
        mapper.insert(book);
        List<Stores> stores = storesMapper.listAll(new Stores());
        for (Stores store : stores) {
            branchMapper.insert(new Branch(store.getStoresId(),book.getBookId(),0));
            sum++;
        }
        return sum;
    }

    @Override
    public int update(Book book) {
        return mapper.update(book);
    }

    @Override
    public int delById(Integer bookId) {
        return mapper.delById(bookId);
    }
}
